package de.iritgo.skillfull.component.motion;

import java.awt.Point;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;

import de.iritgo.skillfull.collision.QuadTree;
import de.iritgo.skillfull.component.Component;
import de.iritgo.skillfull.component.MotionComponentImpl;
import de.iritgo.skillfull.component.visitor.PositionVisitor;
import de.iritgo.skillfull.component.visitor.Visitor;
import de.iritgo.skillfull.config.ConfigManager;
import de.iritgo.skillfull.entity.Entity;

public class MoveRotationComponentImpl extends MotionComponentImpl implements MoveRotationComponent
{
	/* The config manager */
	private ConfigManager configManager;

	private float currentRotation;
	
	private float dstRotation;

	private float duration;
	
	private float speed;
	
	private float nextTick = -1;

	private boolean destinationReached;
	
	
	/* Set the config manager */
	public void setConfigManager(ConfigManager configManager) 
	{
		this.configManager = configManager;
	}

	public float getDstRotation() 
	{
		return dstRotation;
	}

	public void setDstRotation(float dstRotation) 
	{
		this.dstRotation = dstRotation;
	}

	public float getCurrentRotation() 
	{
		return currentRotation;
	}

	public void setCurrentRotation(float rotation) 
	{
		this.currentRotation = rotation;
	}
	
	public float getSpeed () 
	{
		return speed;
	}

	public void setSpeed (float speed) 
	{
		this.speed = speed;
	}

	public float getDuration() 
	{
		return duration;
	}

	public void setDuration(float duration) 
	{
		this.duration = duration;
	}

	@Override
	public void accept (Visitor visitor)
	{
		visitor.visit (this);
		
		if (components == null)
			return;

		for (Component component : components)
		{
			component.accept (visitor);
		}
	}

	public void moveTo (Point destination) 
	{
		this.destinationReached = false;

		int dstX = (int) destination.x;
		int dstY = (int) destination.y;
		
		dstRotation = (float) java.lang.Math.toDegrees (Math.atan2 (x - dstX, y - dstY) * -1);
	}

	public void moveTo (float direction) 
	{
		this.destinationReached = false;

		dstRotation = direction;
	}

	@Override
	public void stop() 
	{
		dstRotation = currentRotation;
	}	
	
	public void render(GameContainer container, Graphics g, Entity entity) 
	{
		if (configManager.functionActive("showEntityRotation"))
		{
			g.drawString("R:" + currentRotation, x + 32, y);
		}
	}
	
	public void update (GameContainer container, int delta, Entity entity)
	{
		/**
		 * Check speed an make a step...
		 */
		
		nextTick -= delta;
		
		while (nextTick < 0 && ! destinationReached)
		{
			nextTick += duration / speed;
			currentRotation = dstRotation;
		}
			// Later real rotation
		if (components == null)
			return;

		for (Component component : components)
		{
			component.update (container, delta, entity);
		}
	}

	public boolean dstReached () 
	{
		if (dstRotation == currentRotation)
		{
			destinationReached = true;
			return true;
		}
		return false;
	}


	@Override
	public void printConfiguration ()
	{
		if (components == null)
			return;

		for (Component component : components)
		{
			component.printConfiguration ();
		}
	}
}
